package shorturl.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import shorturl.base.IShorterEntity;

import java.util.Calendar;
import java.util.Date;

/**
 * 附带有效时长的短链接实体类
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
public class ShorterWithPeriod implements IShorterEntity {

    /**
     * 短链接字符串
     */
    private String shorterString;

    /**
     * 原始链接
     */
    private String url;

    /**
     * 有效时长 秒
     */
    private int period;

    /**
     * 创建时间
     */
    private Date createDate;

    @Override
    public boolean verify(){
        if(createDate == null || period <= 0){
            return false;
        }
        final Calendar c = Calendar.getInstance();
        c.setTime(createDate);
        c.add(Calendar.SECOND, period);
        return new Date().before(c.getTime());
    }
}
